# Load required QCA packages. Note that QCA version 2.1 is used.
library(QCA)


# Insert uncalibrated data set (Supplemental online material S1 shows where this dataset comes from) as follows

datfuzzy <- data.frame(
  Vinitial = c(0.25,0.73,0.80,0.70,0.10,0.10,0.10,0.10,0.50,0.40,0.10,0.53,0.41),
  Iinitial = c(0.39,0.65,0.58,0.36,0.17,0.31,0.41,0.75,0.35,0.24,0.45,0.26,0.65),
  H = c(0,0,1,1,0,0,0,0,0,0,0,0,0),
  T = c(1,1,0,0,0,0,0,0,1,0,0,0,0),
  S = c(1,1,1,1,0,0,0,0,0,0,0,0,0))



rownames(datfuzzy) <- c("TU","EG","LI","SY","LE","JO","MO","AL","YE","OM","KU","BA","SA")

# Alternatively, use the read command to enter the datfuzzy csv file

# Calibrate the Iinitial and Vinitial conditions # See Annex 1 for the calibrated datamatrix 

datfuzzy$V <- calibrate(datfuzzy$Vinitial, type = "fuzzy", thresholds=c(0.15, 0.35, 0.75), logistic = TRUE)
datfuzzy$I <- calibrate(datfuzzy$Iinitial, type = "fuzzy", thresholds=c(0.25, 0.50, 0.60), logistic = TRUE)
datfuzzy



#### ANALYSIS OF NECESSITY #### see supplemental online material S2: tables a and b ####

# Identify necessary conditions for sanctions presence (S)

pof(datfuzzy[, c(3,4,6,7)], outcome = "S", datfuzzy, relation = "nec", ron = TRUE)
pof(1 - datfuzzy[, c(3,4,6,7)], outcome = "S", datfuzzy, relation = "nec", ron = TRUE)

# Identify necessary conditions for sanctions absence (~S)

pof(datfuzzy[, c(3,4,6,7)], neg.out = TRUE, outcome = "S", datfuzzy, relation = "nec", ron = TRUE)
pof(1 - datfuzzy[, c(3,4,6,7)], neg.out = TRUE, outcome = "S", datfuzzy, relation = "nec", ron = TRUE)




#### ANALYSIS OF SUFFICIENCY #### NOTE: THIS PART IS USED TO CREATE TABLES 1-3 ####


## sanctions presence (S)

# create Truth Table # See Table 1

tt <- truthTable(datfuzzy, outcome = "S", conditions = c("V", "I", "H", "T"), incl.cut1 = 0.75, complete = TRUE, show.cases = TRUE, sort.by=c("incl", "n"))
tt

# complex solution and most parsimonious solution # See Table 2

eqmcc(tt, details = TRUE, show.cases = TRUE, use.tilde = TRUE)
eqmcc(tt, details = TRUE, show.cases = TRUE, use.tilde = TRUE, include = "?")

# verify which simplifying assumptions are used to obtain the most parsimonious solutions # See Supplemental online material S3

a <- eqmcc(tt, details = TRUE, show.cases = TRUE, use.tilde = TRUE, include = "?")
a$SA

# intermediate solution # See Table 2

TT2 <- tt
TT2$tt[(TT2$tt$V==0) & (TT2$tt$T==0) & (TT2$tt$OUT=="?"), "OUT"] <- 0
eqmcc(TT2, details = TRUE, show.cases = TRUE, use.tilde = TRUE, include = "?")


## sanctions absence (~S)

tt <- truthTable(datfuzzy, neg.out = TRUE, outcome = "S", conditions = c("V", "I", "H", "T"), incl.cut1 = 0.75, complete = TRUE, show.cases = TRUE, sort.by=c("incl", "n"))

# conservative solution and most parsimonious solution # See Table 3

eqmcc(tt, details = TRUE, show.cases = TRUE, use.tilde = TRUE)
eqmcc(tt, details = TRUE, show.cases = TRUE, use.tilde = TRUE, include = "?")

# verify which simplifying assumptions are used to obtain the most parsimonious solutions # See supplemental online material S3

b <- eqmcc(tt, details = TRUE, show.cases = TRUE, use.tilde = TRUE, include = "?")
b$SA

# intermediate solution (which equals the conservative solution) # See Table 3

TT2 <- tt
TT2$tt[(TT2$tt$V==1) & (TT2$tt$I==0) & (TT2$tt$H==1) & (TT2$tt$T==1) & (TT2$tt$OUT=="?"), "OUT"] <- 0
eqmcc(TT2, details = TRUE, show.cases = TRUE, use.tilde = TRUE, include = "?")






#### ADDITIONAL ANALYSES ####

## Analyse what happens if Yemen is recoded into a positive case # See supplemental online material S4

datfuzzy <- data.frame(
  Vinitial = c(0.25,0.73,0.80,0.70,0.10,0.10,0.10,0.10,0.50,0.40,0.10,0.53,0.41),
  Iinitial = c(0.39,0.65,0.58,0.36,0.17,0.31,0.41,0.75,0.35,0.24,0.45,0.26,0.65),
  H = c(0,0,1,1,0,0,0,0,0,0,0,0,0),
  T = c(1,1,0,0,0,0,0,0,1,0,0,0,0),
  S = c(1,1,1,1,0,0,0,0,1,0,0,0,0))

rownames(datfuzzy) <- c("TU","EG","LI","SY","LE","JO","MO","AL","YE","OM","KU","BA","SA")

# re-enter the same commands as the ones described at the start of this replication file, both for calibration and the analysis of sufficiency (not printed here)
